package com.ky.manage.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.ky.manage.R;
import com.ky.manage.activity.GoodsDetailActivity;
import com.ky.manage.adapter.MountingsAdapter;
import com.ky.manage.base.BaseFragment;
import com.ky.manage.model.response.MountingsResp;
import com.ky.manage.ui.MyGridView;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ky.manage.utils.http.BusinessHttpReqUtils.SERVICE_DOMAIN;

public class MountingsFragment extends BaseFragment {
    /**
     * value 0 is product(产品); value 1 is mountings(配件) /appApi/getMountingsList
     */
    private int mType = 0;
    private MyGridView gridView;
    List<MountingsResp.RowsDTO> mountings;

    public MountingsFragment(){}

    public MountingsFragment(int type) {
        mType = type;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_mountings;
    }

    @Override
    protected void initView(View rootView) {
        gridView = findView(R.id.gv_mountings);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MountingsResp.RowsDTO rowsDTO = mountings.get(position);
                Intent intent = new Intent(getContext(), GoodsDetailActivity.class);
                intent.putExtra("id", rowsDTO.getMountingsId());
                intent.putExtra("type", 2);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        getMountings();
    }

    private void getMountings() {
        AsyncHttpUtils.getInstance().get(SERVICE_DOMAIN + "/appApi/getMountingsList", new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "getMountings onFailure responseString = " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "getMountings onSuccess responseString = " + responseString);
                MountingsResp resp = new Gson().fromJson(responseString, MountingsResp.class);
                if(resp.getCode() == 200) {
                    mountings = resp.getRows();
                    mHandler.sendEmptyMessage(1);
                }

            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            gridView.setAdapter(new MountingsAdapter(mContext, mountings));
        }
    };
}
